import * as TeamApi from '@src/api/TeamApi';

import _ from 'lodash';
import qs from 'qs';
/* enum */
import TenantDataLimitSourceEnum from '@model/enum/TenantDataLimitSourceEnum';
import TenantDataLimitTypeEnum from '@model/enum/TenantDataLimitTypeEnum';
/* mixins */
import VersionMixin from '@src/mixins/versionMixin/index.ts';
/* compoents*/
import TeamEdit from '@src/modules/teamV2/compoent/TeamEdit.vue';
let tag = {};
export default {
  name: 'team-edit-view',
  mixins: [VersionMixin],
  props: {
    initData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      pending: false,
      parent: {}
    };
  },
  components: {
    TeamEdit
  },
  beforeMount() {
    let query = qs.parse(window.location.search.substr(1));
    tag = this.initData && this.initData.tag || {};
    this.parent.id = query.pid;
    this.parent.tagName = query.pname;
    if (this.isCreate) {
      // 检查版本数量限制
      this.checkNumExceedLimitBeforeHandler
        && this.checkNumExceedLimitBeforeHandler(
          TenantDataLimitSourceEnum.Tag,
          TenantDataLimitTypeEnum.Tag
        );
    }
  },
  methods: {
    /* 新建 部门 */
    async teamCreate(res,callback) {
      const params = res.params;
      this.pending = true;
      let child = '';
      try {
        // 判断是否是新建子部门
        if (this.parent.id) {
          params.parent = {
            id: this.parent.id,
            tagName: this.parent.tagName
          };
          child = '子';
        }

        const CreateTeamPromise = TeamApi.createTag(params);
        let result = await CreateTeamPromise
        // let result = await this.checkNumExceedLimitAfterHandler(
        //   CreateTeamPromise
        // );
        this.$platform.notification({
          type: result.status == 0 ? 'success' : 'error',
          title: result.status == 0 ? this.$t('common.base.tip.createSuccess') : this.$t('common.base.tip.createFail'),
          message: result.status == 0 ? null : result.message
        });
        if (result.status == 0) {
            callback(res);
        }
      } catch (error) {
        console.error('error: ', error);
      }

      this.pending = false;
    },
    /* 更新 部门 */
    async teamUpdate(res,callback) {
      const params = res.params;
      this.pending = true;
      try {
        params.id = this.id;
        let result = await TeamApi.updateTag(params);
        this.$platform.notification({
          type: result.status == 0 ? 'success' : 'error',
          title: result.status == 0 ? this.$t("common.base.tip.edit2Success") : this.$t("common.base.tip.editFail"),
          message: result.status == 0 ? null : result.message
        });
        if (result.status == 0) {
          callback(res);
        }
      } catch (error) {
        console.error('error: ', error);
      }

      this.pending = false;
    }
  }
};